package com.link.base.base.fund.controller;

import com.link.base.user.model.CoreUser;
import com.link.base.base.fund.model.BillAccount;
import com.link.base.base.fund.service.BillAccountService;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.controller.BasicControllerException;
import com.link.core.basic.query.Filter;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.StringUtils;
import com.link.core.util.UserUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
/**
 * Copyright  hande Company. All Rights Reserved.
 *
 * @author  unknwon
 * @date:   2018/11/12 10:02
 * @version 1.0
 */

@Controller
@RequestMapping("/link/billAccount")
public class BillAccountController extends BasicController<BillAccount> {

    @Resource
    private BillAccountService billAccountService;

    @Override
    public BasicService<BillAccount> getBasicService() throws Exception {
        return billAccountService;
    }

    @RequestMapping(value = "/queryDmsBill")
    @ResponseBody
    public Map<String, Object> queryDmsDealarAccount(@JsonParam QueryParams qps,@JsonParam BillAccount entity, HttpSession session,
                                                     HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        CoreUser user = UserUtil.getUser();
        if (null == user.getAcctId()) {
            result.put("success", false);
            result.put("result", "无法获取当前用户经销商信息");
            return result;
        }
        qps.addFilter(new Filter("headId0123213131", "accntId", Filter.OP_EQUAL, String.valueOf(user.getAcctId())));
        return this.queryByExamplePage(qps, entity, session, request, response);
    }

    @RequestMapping(value = "/confirmBillAccount")
    @ResponseBody
    public Map<String, Object> confirmBillAccount(@RequestBody BillAccount entity, HttpSession session,
                                                  HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            if (null == entity.getId()) {
                throw new ServiceException("PUBLIC-007");
            }
            CoreUser user = UserUtil.getUser();
            //设置确认人
            entity.setConfirmBy(user.getId());
            billAccountService.confirmBillAccount(entity);
            BillAccount newEntry = getBasicService().queryById(entity);
            result.put("newRow", newEntry);
            result.put("success", true);
        } catch (BasicServiceException e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
            result.put("detailMessage", e.getDetailMessage());
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/rejectBillAccount")
    @ResponseBody
    public Map<String, Object> rejectBillAccount(@RequestBody BillAccount entity, HttpSession session,
                                                 HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            if (null == entity.getId()) {
                throw new ServiceException("PUBLIC-007");
            }
            billAccountService.rejectBillAccount(entity);
            BillAccount newEntry = getBasicService().queryById(entity);
            result.put("newRow", newEntry);
            result.put("success", true);
        } catch (BasicServiceException e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
            result.put("detailMessage", e.getDetailMessage());
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }


    /***
     * 更新对账单状态
     * @param billAccountId  对账单id
     * @param status  状态参数
     * @return
     */
    @RequestMapping(value = "/updateStatus/{billAccountId}/{status}", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateStatus(@PathVariable Long billAccountId, @PathVariable String status) {
        Map<String, Object> result = new HashMap<>(8);
        try {
            if (null == billAccountId || StringUtils.isBlank(status)) {
                throw new BasicControllerException("状态更新数据异常...");
            }
            // 对账单
            BillAccount ba = new BillAccount();
            ba.setId(billAccountId);
            ba = billAccountService.queryById(ba);
            if (Objects.isNull(ba)) {
                throw new BasicControllerException("操作对账单数据不存在...");
            }
            billAccountService.updateStatus(ba, status);
            ba = billAccountService.queryById(ba);
            result.put("success", true);
            result.put("newRow", ba);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }


    /**
     * 汇总对账单 余额
     * @param entity
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/sumDealerBalance")
    @ResponseBody
    public Map<String, Object> sumDealerBalance(@RequestBody BillAccount entity, HttpSession session,
                                                 HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            entity =  billAccountService.sumDealerBalance(entity);
            result.put("newRow", entity);
            result.put("success", true);
        } catch (BasicServiceException e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
            result.put("detailMessage", e.getDetailMessage());
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

}
